﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Gui 对象 - 方法 &amp; 属性 | AutoHotkey v2</title>
<meta name="description" content="The Gui object provides an interface for a GUI window to perform actions, such as adding new controls, or to retrieve or set values, such as colors." />
<meta name="ahk:equiv-v1" content="commands/Gui.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css">
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Gui 对象</h1>
<p>提供用于创建和管理窗口以及创建控件的接口. 这些窗口可以用作数据输入表单或自定义用户界面.</p>
<p>All Gui objects are instances of the <code>Gui</code> class. Gui objects can be created with <a href="../objects/Gui.htm#New">Gui.New()</a> and retrieved with <a href="../commands/GuiFromHwnd.htm">GuiFromHwnd</a>.</p>

<p><strong>Static Methods:</strong></p>
<ul>
  <li><a href="#New">New</a>: Creates a new Gui object.</li>
</ul>
<p><strong>属性:</strong></p>
<ul>
  <li><a href="#BackColor">BackColor</a>: 检索或设置窗口的背景色.</li>
  <li><a href="#__Item">__Item</a>: 检索与指定名称, 文本, ClassNN 或 HWND 关联的 <a href="GuiControl.htm">GuiControl 对象</a>.</li>
  <li><a href="#FocusedCtrl">FocusedCtrl</a>: 检索 GUI 的焦点控件的 <a href="GuiControl.htm">GuiControl 对象</a>.</li>
  <li><a href="#Hwnd">Hwnd</a>: 检索 GUI 窗口的窗口句柄(HWND).</li>
  <li><a href="#MarginX">MarginX</a>: 检索或设置两侧与随后创建控件之间的水平边距的大小.</li>
  <li><a href="#MarginY">MarginY</a>: 检索或设置两侧与随后创建控件之间的垂直边距的大小.</li>
  <li><a href="#MenuBar">MenuBar</a>: 检索或设置窗口的菜单栏.</li>
  <li><a href="#Name">Name</a>: 检索或设置 GUI 窗口的自定义名称.</li>
  <li><a href="#Title">Title</a>: 检索或设置 GUI 的标题.</li>
</ul>
<p><strong>方法:</strong></p>
<ul>
  <li><a href="#Add">Add</a>: 创建文本, 按钮或复选框等控件.</li>
  <li><a href="#Destroy">Destroy</a>: 删除窗口.</li>
  <li><a href="#Flash">Flash</a>: 闪烁窗口及其任务栏按钮.</li>
  <li><a href="#GetClientPos">GetClientPos</a>: Retrieves the position and size of the window&#39;s client area.</li>
  <li><a href="#GetPos">GetPos</a>: Retrieves the position and size of the window.</li>
  <li><a href="#Hide">Hide / Cancel</a>: 隐藏窗口.</li>
  <li><a href="#Maximize">Maximize</a>: 打开并最大化窗口.</li>
  <li><a href="#Minimize">Minimize</a>: 打开并最小化窗口.</li>
  <li><a href="#Move">Move</a>: Moves and/or resizes the GUI window.</li>
  <li><a href="#__Enum">__Enum</a>: 允许遍历 GUI 的控件.</li>
  <li><a href="GuiOnEvent.htm">OnEvent</a>: 注册一个函数或方法, 当给定的事件被触发时调用.</li>
  <li><a href="#Opt">Opt</a>: 为窗口的外观和行为设置各种选项和样式.</li>
  <li><a href="#Restore">Restore</a>: 如果窗口事先最小化或最大化, 则打开并还原窗口.</li>
  <li><a href="#SetFont">SetFont</a>: 设置随后创建的控件的字体, 大小, 样式, 和文本颜色.</li>
  <li><a href="#Show">Show</a>: 显示窗口. 它还可以最小化, 最大化或移动窗口.</li>
  <li><a href="#Submit">Submit</a>:  Collects the values from named controls and composes them into an <a href="Object.htm">Object</a>. 可选择性地隐藏窗口.</li>
</ul>
<p><strong>常规:</strong></p>
<ul>
  <li><a href="#Navigate">键盘导航</a></li>
  <li><a href="#Appear">窗口外观</a></li>
  <li><a href="#GenRemarks">常规备注</a></li>
  <li><a href="#Related">相关</a></li>
  <li><a href="#Examples">示例</a></li>
</ul>

<p class="note"><strong>Note:</strong> The name <code>MyGui</code> is used here for instances of the Gui class, but any other valid variable name can be used. The name <code>Gui</code> should not be used, except to refer to the Gui class itself.</p>

<div class="methodShort" id="New">
<h2>Gui.New</h2>
<p>Creates and returns a new Gui object.</p>
<pre class="Syntax">MyGui := <span class="func">Gui.New</span>(<span class="optional">Options, Title := <a href="../Variables.htm#ScriptName">A_ScriptName</a>, EventObj</span>)</pre>
<dl>
  <dt>Options</dt><dd>
    <p>Type: <a href="../Concepts.htm#strings">String</a></p>
    <p>This parameter can contain any of the options supported by <a href="#Opt">Gui.Opt</a>.</p>
  </dd>
  <dt>Title</dt><dd>
    <p>Type: <a href="../Concepts.htm#strings">String</a></p>
    <p>The window title. If omitted, it defaults to the current value of <a href="../Variables.htm#ScriptName">A_ScriptName</a>.</p>
  </dd>
  <dt id="EventObj">EventObj</dt><dd>
    <p>Type: <a href="../Concepts.htm#objects">Object</a></p>
    <p>An "event sink", or object to bind events to. If <em>EventObj</em> is specified, <a href="GuiOnEvent.htm">OnEvent</a>, OnNotify and OnCommand can be used to register methods of <em>EventObj</em> to be called when an event is raised. If omitted or empty, any string passed to OnEvent's <em>Function</em> parameter is interpreted as a function name.</p>
  </dd>
</dl>
</div>

<div class="methodShort" id="__New">
<h2>__New</h2>
<p>Constructs a new Gui instance.</p>
<pre class="Syntax">MyGui.<span class="func">__New</span>(<span class="optional">Options, Title := <a href="../Variables.htm#ScriptName">A_ScriptName</a>, EventObj</span>)</pre>
<p>A Gui subclass may override __New and call <code>super.__New(Options, Title, this)</code> to handle its own events. In such cases, events for the main window (such as Close) do not pass an explicit Gui parameter, as <code>this</code> already contains a reference to the Gui.</p>
<p>An exception is thrown if the window has already been constructed or destroyed.</p>
</div>

<div class="methodShort" id="Add">
<h2>Add</h2>
<p>Adds a control to the GUI window, and returns a <a href="GuiControl.htm">GuiControl object</a>.</p>
<pre class="Syntax">MyGui.<span class="func">Add</span>(ControlType <span class="optional">, Options, Text</span>)
MyGui.<span class="func">Add</span>ControlType(<span class="optional">Options, Text</span>)</pre>
<dl>
  <dt>ControlType</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>This is one of the following: <a href="../commands/GuiControls.htm#Text">Text</a>, <a href="../commands/GuiControls.htm#Edit">Edit</a>, <a href="../commands/GuiControls.htm#UpDown">UpDown</a>, <a href="../commands/GuiControls.htm#Picture">Picture</a>, <a href="../commands/GuiControls.htm#Button">Button</a>, <a href="../commands/GuiControls.htm#Checkbox">Checkbox</a>, <a href="../commands/GuiControls.htm#Radio">Radio</a>, <a href="../commands/GuiControls.htm#DropDownList">DropDownList</a>, <a href="../commands/GuiControls.htm#ComboBox">ComboBox</a>, <a href="../commands/GuiControls.htm#ListBox">ListBox</a>, <a href="../commands/ListView.htm">ListView</a>, <a href="../commands/TreeView.htm">TreeView</a>, <a href="../commands/GuiControls.htm#Link">Link</a>, <a href="../commands/GuiControls.htm#Hotkey">Hotkey</a>, <a href="../commands/GuiControls.htm#DateTime">DateTime</a>, <a href="../commands/GuiControls.htm#MonthCal">MonthCal</a>, <a href="../commands/GuiControls.htm#Slider">Slider</a>, <a href="../commands/GuiControls.htm#Progress">Progress</a>, <a href="../commands/GuiControls.htm#GroupBox">GroupBox</a>, <a href="../commands/GuiControls.htm#Tab">Tab</a>, <a href="../commands/GuiControls.htm#StatusBar">StatusBar</a>, <a href="../commands/GuiControls.htm#ActiveX">ActiveX</a>, <a href="../commands/GuiControls.htm#Custom">Custom</a></p>
  <p>例如:</p>
  <pre>MyGui := Gui.New()
MyGui.Add("Text",, "Please enter your name:")
MyGui.AddEdit("vName")
MyGui.Show</pre></dd>
  <dt>Options</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p id="PosSize"><strong>Positioning and Sizing of Controls</strong></p>
  <p>If some dimensions and/or coordinates are omitted from <em>Options</em>, the control will be positioned relative to the previous control and/or sized automatically according to its nature and contents.</p>
  <p>The following options are supported:</p>
  <p id="R"><strong>R</strong>: Rows of text (can contain a floating point number such as R2.5). <strong>R</strong> is often preferable to specifying <strong>H</strong> (Height). If both the <strong>R</strong> and <strong>H</strong> options are present, <strong>R</strong> will take precedence. For a GroupBox, this setting is the number of controls for which to reserve space inside the box. For <a href="../commands/GuiControls.htm#DropDownList">DropDownLists</a>, <a href="../commands/GuiControls.htm#ComboBox">ComboBoxes</a>, and <a href="../commands/GuiControls.htm#ListBox">ListBoxes</a>, it is the number of items visible at one time inside the list portion of the control (but it is often desirable to omit both the <strong>R</strong> and <strong>H</strong> options for DropDownList and ComboBox, as the popup list will automatically take advantage of the available height of the user's desktop). For other control types, <strong>R</strong> is the number of rows of text that can visibly fit inside the control.</p>
  <p><strong>W</strong>: Width, in pixels. If omitted, the width is calculated automatically for some control types based on their contents. The other controls types have the following default widths:<br>
      Tab controls: 30 times the current font size, plus 3 times the <a href="#MarginX">X-margin</a>.<br>
      Vertical Progress Bars: Two times the current font size.<br>
      Horizontal Progress Bars, horizontal Sliders, DropDownLists, ComboBoxes, ListBoxes, GroupBoxes,  Edits, and Hotkeys: 15 times the current font size (except GroupBoxes, which multiply by 18 to provide room inside for margins).</p>
  <p><strong>H</strong>: Height, in pixels. If both the <strong>H</strong> and <strong>R</strong> options are absent, DropDownLists, ComboBoxes, ListBoxes, and empty multi-line Edit controls default to 3 rows; GroupBoxes default to 2 rows; vertical Sliders and Progress Bars default to 5 rows; horizontal Sliders default to 30 pixels (except if a thickness has been specified); horizontal Progress Bars default to 2 times the current font size; Hotkey controls default to 1 row; and Tab controls default to 10 rows. For the other control types, the height is calculated automatically based on their contents. Note that for DropDownLists and ComboBoxes, <strong>H</strong> is the combined height of the control's always-visible portion and its list portion (but even if the height is set too low, at least one item will always be visible in the list). Also, for all types of controls, specifying the number of rows via the <strong>R</strong> option is usually preferable to using <strong>H</strong> because it prevents a control from showing partial/incomplete rows of text.</p>
  <p><strong>wp+n</strong>, <strong>hp+n</strong>, <strong>wp-n</strong>, <strong>hp-n</strong> (where <strong>n</strong> is any number) can be used to set the width and/or height of a control equal to the previously added control's width or height, with an optional plus or minus adjustment. 例如, <code>wp</code> would set a control's width to that of the previous control, and <code>wp-50</code> would set it equal to 50 less than that of the previous control.</p>
  <p id="XY"><strong>X</strong>: X-position. 例如, specifying <code>x0 y0</code> would position the control in the upper left corner of the window's client area, which is the area beneath the title bar and menu bar (if any). If <strong>X</strong> is omitted but not <strong>Y</strong>, the control will be positioned to the right of all previously added controls, which can be thought of as starting a new "column".</p>
  <p><strong>Y</strong>: Y-position. If <strong>Y</strong> is omitted but not <strong>X</strong>, the control will be positioned beneath all previously added controls, which can be thought of as starting a new "row".</p>
  <p>Omitting either <strong>X</strong>, <strong>Y</strong> or both is useful to make a GUI layout automatically adjust to any future changes you might make to the size of controls or font. By contrast, specifying an absolute position for every control might require you to manually shift the position of all controls that lie beneath and/or to the right of a control that is being enlarged or reduced.</p>
  <p>If both <strong>X</strong> and <strong>Y</strong> are omitted, the control will be positioned beneath the previous control using a standard padding distance.</p>
  <p id="PosPlus">For <strong>X</strong> and <strong>Y</strong>, an optional plus sign can be included to position a control relative to the right or bottom edge (respectively) of the control that was previously added. 例如, specifying <code>Y+10</code> would position the control 10 pixels beneath the bottom of the previous control rather than using the standard padding distance. Similarly, specifying <code>X+10</code> would position the control 10 pixels to the right of the previous control's right edge. Since negative numbers such as <code>X-10</code> are reserved for absolute positioning, to use a negative offset, include a plus sign in front of it. 例如: <code>X+-10</code>.</p>
  <p id="PosPlusMargin">For <strong>X+</strong> and <strong>Y+</strong>, the letter <strong>M</strong> can be used as a substitute for the window's current <a href="#MarginX">margin</a>. 例如, <strong>x+m</strong> uses the right edge of the previous control plus the standard padding distance. <strong>xp y+m</strong> positions a control below the previous control, whereas specifying an X coordinate on its own would normally imply <strong>yp</strong> by default.</p>
  <p id="xp"><strong>xp+n</strong>, <strong>yp+n</strong>, <strong>xp-n</strong>, <strong>yp-n</strong> (where <strong>n</strong> is any number) can be used to position controls relative to the previous control's upper left corner, which is often useful for enclosing controls in a <a href="../commands/GuiControls.htm#GroupBox">GroupBox</a>.</p>
  <p id="xm"><strong>xm</strong> and <strong>ym</strong> can be used to position a control at the leftmost and topmost <a href="#MarginX">margins</a> of the window, respectively (these two may also be followed by a plus/minus sign and a number). By specifying <strong>ym</strong> without any x-position at all, the control will be positioned at the top margin but to the right of all previously added controls, which can be thought of as starting a new "column". The converse is also true.</p>
  <p id="xs"><strong>xs</strong> and <strong>ys</strong>: these are similar to <strong>xm</strong> and <strong>ym</strong> except that they refer to coordinates that were saved by having previously added a control with the word <a href="#Section">Section</a> in its options (the first control of the window always starts a new section, even if that word isn't specified in its options). By specifying <strong>ys</strong> without any x-position at all, the control will be positioned at the previously saved y-position, but to the right of all controls that have been added since the most recent use of the word <a href="#Section">Section</a>; this can be thought of as starting a new column within the section. 例如:</p>
  <pre>MyGui := Gui.New()
MyGui.Add("Edit", "w600")  <em>; Add a fairly wide edit control at the top of the window.</em>
MyGui.Add("Text", "<strong>section</strong>", "First Name:")  <em>; Save this control's position and start a new section.</em>
MyGui.Add("Text",, "Last Name:")
MyGui.Add("Edit", "<strong>ys</strong>")  <em>; Start a new column within this section.</em>
MyGui.Add("Edit")
MyGui.Show</pre>
  <p>The converse of the above (specifying <strong>xs</strong> but omitting the y-position) is also true.</p>
  <p><strong>xs</strong> and <strong>ys</strong> may optionally be followed by a plus/minus sign and a number. Also, it is possible to specify both the word <a href="#Section">Section</a> and xs/ys in a control's options; this uses the previous section for itself but establishes a new section for subsequent controls.</p>
  
  <p id="Events"><strong>Storing and Responding to User Input</strong></p>
  <p id="var"><strong>V</strong>: Sets the control's <a href="GuiControl.htm#Name">Name</a>. Specify the name immediately after the letter V, which is not included in the name. 例如, specifying <code><strong>v</strong>MyEdit</code> would name the control "MyEdit".</p>
  <p id="label"><strong>Events</strong>: Event handlers (such as a function which is called automatically when the user clicks or changes a control) cannot be set within the control's <em>Options</em>. Instead, <a href="GuiOnEvent.htm">OnEvent</a> can be used to register a callback function or method for each event of interest.</p>
  
  <p id="OtherOptions"><strong>Controls: Common Styles and Other Options</strong></p>
  <p>Note: In the absence of a preceding sign, a plus sign is assumed; 例如, <code>Wrap</code> is the same as <code>+Wrap</code>. By contrast, <code>-Wrap</code> would remove the word-wrapping property.</p>
  <p id="AltSubmit"><strong>AltSubmit</strong>: Uses alternate submit method. For DropDownList, ComboBox, ListBox and Tab, this causes <a href="#Submit">Gui.Submit</a> to store the position of the selected item rather than its text. If no item is selected, a ComboBox will still store the text of its edit field.</p>
  <p><strong>C</strong>: Color of text (has no effect on <a href="../commands/GuiControls.htm#Button">buttons</a>). Specify the letter C followed immediately by a color name (see <a href="../misc/Colors.htm">color chart</a>) or RGB value (the 0x prefix is optional). Examples: <code>cRed</code>, <code>cFF2211</code>, <code>c0xFF2211</code>, <code>cDefault</code>.</p>
  <p id="Disabled"><strong>Disabled</strong>: Makes an input-capable control appear in a disabled state, which prevents the user from focusing or modifying its contents. Use <a href="GuiControl.htm#Enabled">GuiCtrl.Enabled</a> to enable it later. Note: To make an Edit control read-only, specify the string <code>ReadOnly</code> instead. Also, the word Disabled may optionally be followed immediately by a 0 or 1 to indicate the starting state (0 for enabled and 1 for disabled). In other words, <code>Disabled</code> and <code>"Disabled" VarContainingOne</code> are the same.</p>
  <p><strong>Hidden</strong>: The control is initially invisible. Use <a href="GuiControl.htm#Visible">GuiCtrl.<span style="color: #4280ca;">Visible</span></a> to show it later. The word Hidden may optionally be followed immediately by a 0 or 1 to indicate the starting state (0 for visible and 1 for hidden).  In other words, <code>Hidden</code> and <code>"Hidden" VarContainingOne</code> are the same.</p>
  <p><strong>Left</strong>: Left-justifies the control's text within its available width. This option affects the following controls: Text, Edit, Button, Checkbox, Radio, UpDown, Slider, Tab, Tab2, GroupBox, DateTime.</p>
  <p><strong>Right</strong>: Right-justifies the control's text within its available width. For checkboxes and radio buttons, this also puts the box itself on the right side of the control rather than the left. This option affects the following controls: Text, Edit, Button, Checkbox, Radio, UpDown, Slider, Tab, Tab2, GroupBox, DateTime, Link.</p>
  <p><strong>Center</strong>: Centers the control's text within its available width. This option affects the following controls: Text, Edit, Button, Checkbox, Radio, Slider, GroupBox.</p>
  <p id="Section"><strong>Section</strong>: Starts a new section and saves this control's position for later use with the <em>xs</em> and <em>ys</em> positioning options described <a href="#xs">above</a>.</p>
  <p id="Tabstop"><strong>Tabstop</strong>: Use <code>-Tabstop</code> (i.e. minus Tabstop) to have an input-capable control skipped over when the user presses the <kbd>Tab</kbd> key to navigate.</p>
  <p id="Wrap"><strong>Wrap</strong>: Enables word-wrapping of the control's contents within its available width. Since nearly all control types start off with word-wrapping enabled, use <code>-Wrap</code> to disable word-wrapping.</p>
  <p><strong>VScroll</strong>: Provides a vertical scroll bar if appropriate for this type of control.</p>
  <p><strong>HScroll</strong>: Provides a horizontal scroll bar if appropriate for this type of control. The rest of this paragraph applies to <a href="../commands/GuiControls.htm#ListBox">ListBox</a> only. The horizontal scrolling width defaults to 3 times the width of the ListBox. To specify a different scrolling width, include a number immediately after the word HScroll. 例如, <code>HScroll500</code> would allow 500 pixels of scrolling inside the ListBox. However, if the specified scrolling width is smaller than the width of the ListBox, no scroll bar will be shown (though the mere presence of <em>HScroll</em> makes it possible for the horizontal scroll bar to be added later via <code>MyScrollBar.<a href="GuiControl.htm#Opt">Opt</a>("+HScroll500")</code>, which is otherwise impossible).</p>
  
  <p><strong>Controls: Uncommon Styles and Options</strong></p>
  <p id="BackgroundTrans"><strong>BackgroundTrans</strong>: Uses a transparent background, which allows any control that lies behind a Text, Picture, or GroupBox control to show through. 例如, a transparent Text control displayed on top of a Picture control would make the text appear to be part of the picture. Use <code>GuiCtrl.<a href="GuiControl.htm#Opt">Opt</a>("+Background")</code> to remove this option later. See <a href="../commands/GuiControls.htm#PicAltSubmit">Picture control's AltSubmit section</a> for more information about transparent images. Known limitation: BackgroundTrans might not work properly for controls inside a <a href="../commands/GuiControls.htm#Tab">Tab control</a> that contains a <a href="../commands/ListView.htm">ListView</a>. If a control type does not support this option, an error is thrown.</p>
  <p id="Background"><strong>Background</strong><em>Color</em>: Changes the background color of the control. Replace <em>Color</em> with a color name (see <a href="../misc/Colors.htm">color chart</a>) or RGB value (the 0x prefix is optional). Examples: <code>BackgroundSilver</code>, <code>BackgroundFFDD99</code>. If this option is not present, a <a href="../commands/GuiControls.htm#Text">Text</a>, <a href="../commands/GuiControls.htm#Picture">Picture</a>, <a href="../commands/GuiControls.htm#GroupBox">GroupBox</a>, <a href="../commands/GuiControls.htm#Checkbox">CheckBox</a>, <a href="../commands/GuiControls.htm#Radio">Radio</a>, <a href="../commands/GuiControls.htm#Slider">Slider</a>, <a href="../commands/GuiControls.htm#Tab">Tab</a> or <a href="../commands/GuiControls.htm#Link">Link</a> control initially defaults to the background color set by <a href="#BackColor">Gui.BackColor</a> (or if none or other control type, the system's default background color). Specifying <code>BackgroundDefault</code> or <code>-Background</code> applies the system's default background color. 例如, a control can be restored to the default color via <code>LV.Opt("+BackgroundDefault")</code>. Using <code>+Background</code> without specifying a color reverts <code>-Background</code>. If a control type does not support this option, an error is thrown.</p>
  <p><strong>Border</strong>: Provides a thin-line border around the control. Most controls do not need this because they already have a type-specific border. When adding a border to an <em>existing</em> control, it might be necessary to increase the control's width and height by 1 pixel.</p>
  <p><strong>Theme</strong>: This option can be used to override the window's current theme setting for the newly created control. It has no effect when used on an existing control; however, this may change in a future version. See GUI's <a href="#Theme">+/-Theme</a> option for details.</p>
  <p><strong>(Unnamed Style)</strong>: Specify a plus or minus sign followed immediately by a decimal or hexadecimal <a href="../misc/Styles.htm">style number</a>. If the sign is omitted, a plus sign is assumed.</p>
  <p><strong>(Unnamed ExStyle)</strong>: Specify a plus or minus sign followed immediately by the letter E and a decimal or hexadecimal extended style number. If the sign is omitted, a plus sign is assumed. 例如, <code>E0x200</code> would add the WS_EX_CLIENTEDGE style, which provides a border with a sunken edge that might be appropriate for pictures and other controls. Although the other extended styles are not documented here (since they are rarely used), they can be discovered by searching for WS_EX_CLIENTEDGE at <a href="http://www.microsoft.com">www.microsoft.com</a>.</p></dd>
  <dt>Text</dt><dd>Depending on the specified control type, a string, number or an array.</dd>
</dl>
</div>

<div class="methodShort" id="Show">
<h2>Show</h2>
<p>By default, this makes the window visible, unminimizes it (if necessary) and <a href="../commands/WinActivate.htm">activates</a> it.</p>
<pre class="Syntax">MyGui.<span class="func">Show</span>(<span class="optional">Options</span>)</pre>
<dl>
  <dt>Options</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>Omit the X, Y, W, and H options below to have the window retain its previous size and position. If there is no previous position, the window will be auto-centered in one or both dimensions if the X and/or Y options mentioned below are absent. If there is no previous size, the window will be auto-sized according to the size and positions of the controls it contains.</p>
  <p>Zero or more of the following strings may be present in <em>Options</em> (specify each number as decimal, not hexadecimal):</p>
  <p><strong>Wn</strong>: Specify for <strong>n</strong> the width (in pixels) of the window's client area (the client area excludes the window's borders, title bar, and <a href="#MenuBar">menu bar</a>).</p>
  <p><strong>Hn</strong>: Specify for <strong>n</strong> the height of the window's client area, in pixels.</p>
  <p><strong>Xn</strong>: Specify for <strong>n</strong> the window's X-position on the screen, in pixels. Position 0 is the leftmost column of pixels visible on the screen.</p>
  <p><strong>Yn</strong>: Specify for <strong>n</strong> the window's Y-position on the screen, in pixels. Position 0 is the topmost row of pixels visible on the screen.</p>
  <p><strong>Center</strong>: Centers the window horizontally and vertically on the screen.</p>
  <p><strong>xCenter</strong>: Centers the window horizontally on the screen. 例如: <code>MyGui.Show("xCenter y0")</code>.</p>
  <p><strong>yCenter</strong>: Centers the window vertically on the screen.</p>
  <p id="AutoSize"><strong>AutoSize</strong>: Resizes the window to accommodate only its currently visible controls. This is useful to resize the window after new controls are added, or  existing controls are resized, hidden, or unhidden. 例如:<br>
    <code>MyGui.Show("AutoSize Center")</code></p>
  <p><em><strong>One of the following may also be present:</strong></em></p>
  <p><strong>Minimize</strong>: Minimizes the window and activates the one beneath it.</p>
  <p><strong>Maximize</strong>: Maximizes and activates the window.</p>
  <p><strong>Restore</strong>: Unminimizes or unmaximizes the window, if necessary. The window is also shown and activated, if necessary.</p>
  <p><strong>NoActivate</strong>: Unminimizes or unmaximizes the window, if necessary. The window is also shown without activating it.</p>
  <p><strong>NA</strong>: Shows the window without activating it. If the window is minimized, it will stay that way but will probably rise higher in the z-order (which is the order seen in the alt-tab selector). If the window was previously hidden, this will probably cause it to appear on top of the active window even though the active window is not deactivated.</p>
  <p><strong>Hide</strong>: Hides the window and activates the one beneath it. This is identical in function to <a href="#Hide">MyGui.Hide</a> except that it allows a hidden window to be moved or resized without showing it. 例如: <code>MyGui.Show("Hide x55 y66 w300 h200")</code>.</p></dd>
</dl>
</div>

<div class="methodShort" id="Submit">
<h2>Submit</h2>
<p>Collects the values from named controls and composes them into an <a href="Object.htm">Object</a>. Optionally hides the window.</p>
<pre class="Syntax">NamedCtrlContents := MyGui.<span class="func">Submit</span>(<span class="optional">Hide := true</span>)</pre>
<dl>
  <dt>Hide</dt><dd>
    <p>类型: <a href="../Concepts.htm#boolean">整数(布尔值)</a></p>
    <p>If omitted or 1 (true), the window will be hidden. If 0 (false), the window will not be hidden.</p>
  </dd>
</dl>
<p>The returned object contains one own property per named control, like <code>NamedCtrlContents.%GuiCtrl.<a href="GuiControl.htm#Name">Name</a>% := GuiCtrl.<a href="GuiControl.htm#Value">Value</a></code>, with the exceptions noted below. Only input-capable controls which support <a href="GuiControl.htm#Value">GuiCtrl.Value</a> and have been given a name are included. Use <code>NamedCtrlContents.NameOfControl</code> to retrieve an individual value or <a href="Object.htm#OwnProps">OwnProps</a> to enumerate them all.</p>
<p>For <a href="../commands/GuiControls.htm#DropDownList">DropDownList</a>, <a href="../commands/GuiControls.htm#ComboBox">ComboBox</a>, <a href="../commands/GuiControls.htm#ListBox">ListBox</a> and <a href="../commands/GuiControls.htm#Tab">Tab</a>, the text of the selected item/tab is stored instead of its position number if the control <strong>lacks</strong> the <a href="#AltSubmit">AltSubmit</a> option, or if the ComboBox's text does not match a list item. Otherwise, <a href="GuiControl.htm#Value">Value</a> (the item's position number) is stored.</p>
<p id="submit-radio">If only one <a href="../commands/GuiControls.htm#Radio">Radio</a> button in a radio group has a name, Submit stores the number of the currently selected button instead of the control's <a href="GuiControl.htm#Value">Value</a>. 1 is the first radio button (according to original creation order), 2 is the second, and so on. If there is no button selected, 0 is stored.</p>
<p>Excluded because they are not input-capable: <a href="../commands/GuiControls.htm#Text">Text</a>, <a href="../commands/GuiControls.htm#Pic">Pic</a>, <a href="../commands/GuiControls.htm#GroupBox">GroupBox</a>, <a href="../commands/GuiControls.htm#Button">Button</a>, <a href="../commands/GuiControls.htm#Progress">Progress</a>, <a href="../commands/GuiControls.htm#Link">Link</a>, <a href="../commands/GuiControls.htm#StatusBar">StatusBar</a>.</p>
<p>Also excluded: <a href="../commands/ListView.htm">ListView</a>, <a href="../commands/TreeView.htm">TreeView</a>, <a href="../commands/GuiControls.htm#ActiveX">ActiveX</a>, <a href="../commands/GuiControls.htm#Custom">Custom</a>.</p>
</div>

<div class="methodShort" id="Hide">
<h2>Hide</h2>
<p>Hides the window.</p>
<pre class="Syntax">MyGui.<span class="func">Hide</span>()</pre>
</div>

<div class="methodShort" id="Destroy">
<h2>Destroy</h2>
<p>Removes the window and all its controls, freeing the corresponding memory and system resources.</p>
<pre class="Syntax">MyGui.<span class="func">Destroy</span>()</pre>
<p>If <code>MyGui.Destroy()</code> is not used, the window is automatically destroyed when the Gui object is deleted (see <a href="#deleted">General Remarks</a> for details). All GUI windows are automatically destroyed when the script exits.</p>
</div>

<div class="methodShort" id="SetFont">
<h2>SetFont</h2>
<p>Sets the font typeface, size, style, and/or color for controls added to the window from this point onward.</p>
<p>Note: Omit both parameters to restore the font to the system's default GUI typeface, size, and color. Otherwise, any font attributes which are not specified will be copied from the previous font.</p>
<pre class="Syntax">MyGui.<span class="func">SetFont</span>(<span class="optional">Options, FontName</span>)</pre>
<dl>
  <dt>Options</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>Zero or more options. Each option is either a single letter immediately followed by a value, or a single word. To specify more than one option, include a space between each. 例如: <code>cBlue s12 bold</code>.</p>
  <p>The following words are supported: <strong>bold</strong>, <em>italic</em>, <s>strike</s>, <u>underline</u>, and norm. <em>Norm</em> returns the font to normal weight/boldness and  turns off italic, strike, and underline (but it retains the existing color and size). It is possible to use norm to turn off all attributes and then selectively turn on others. 例如, specifying <code>norm italic</code> would set the font to normal then to italic.</p>
  <p><strong>C</strong>: Color name (see <a href="../misc/Colors.htm">color chart</a>) or RGB value -- or specify the word Default to return to the system's default color (black on most systems). Example values: <code>cRed</code>, <code>cFFFFAA</code>, <code>cDefault</code>. Note: <a href="../commands/GuiControls.htm#Button">Buttons</a> do not obey custom colors. Also, an individual control can be created with a font color other than the current one by including the C option. 例如: <code>MyGui.Add("Text", "cRed", "My Text")</code>.</p>
  <p><strong>S</strong>: Size (in points). 例如: <code>s12</code> (specify decimal, not hexadecimal)</p>
  <p><strong>W</strong>: Weight (boldness), which is a number between 1 and 1000 (400 is normal and 700 is bold). 例如: <code>w600</code> (specify decimal, not hexadecimal)</p>
  <p id="fontq"><strong>Q</strong>: Text rendering quality. 例如: <code>q3</code>. Q should be followed by a number from the following table:</p>
  <table class="info">
    <tr>
      <td>0 = DEFAULT_QUALITY</td>
      <td>Appearance of the font does not matter.</td>
    </tr><tr>
      <td>1 = DRAFT_QUALITY</td>
      <td>Appearance of the font is less important than when the PROOF_QUALITY value is used.</td>
    </tr><tr>
      <td>2 = PROOF_QUALITY</td>
      <td>Character quality of the font is more important than exact matching of the logical-font attributes.</td>
    </tr><tr>
      <td>3 = NONANTIALIASED_QUALITY</td>
      <td>Font is never antialiased, that is, font smoothing is not done.</td>
    </tr><tr>
      <td>4 = ANTIALIASED_QUALITY</td>
      <td>Font is antialiased, or smoothed, if the font supports it and the size of the font is not too small or too large.</td>
    </tr><tr>
      <td>5 = CLEARTYPE_QUALITY</td>
      <td>If set, text is rendered (when possible) using ClearType antialiasing method.</td>
    </tr>
  </table>
  <p>For more details of what these values mean, 请参阅 <a href="http://msdn.microsoft.com/en-us/library/dd183499.aspx">MSDN: CreateFont</a>.</p>
  <p>Since the highest quality setting is usually the default, this feature is more typically used to disable anti-aliasing in specific cases where doing so makes the text clearer.</p></dd>
  <dt>FontName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p><em>FontName</em> may be the name of any font, such as one from the <a href="../misc/FontsStandard.htm">font table</a>. If <em>FontName</em> is omitted or does not exist on the system, the previous font's typeface will be used (or if none, the system's default GUI typeface). This behavior is useful to make a GUI window have a similar font on multiple systems, even if some of those systems lack the preferred font. 例如, by using the following methods in order, Verdana will be given preference over Arial, which in turn is given preference over MS sans serif:</p>
  <pre>MyGui.SetFont(, "MS sans serif")
MyGui.SetFont(, "Arial")
MyGui.SetFont(, "Verdana")  <em>; Preferred font.</em></pre></dd>
</dl>
<p>On a related note, the operating system offers standard dialog boxes that prompt the user to pick a font, color, or icon. These dialogs can be displayed via <a href="../commands/DllCall.htm">DllCall</a> as demonstrated at <a href="https://github.com/majkinetor/mm-autohotkey/tree/master/Dlg">GitHub</a>.</p>
</div>

<div class="methodShort" id="BackColor">
<h2>BackColor</h2>
<p>Retrieves or sets the background color of the window.</p>
<pre class="Syntax">RetrievedColor := MyGui.BackColor</pre>
<pre class="Syntax">MyGui.BackColor := NewColor</pre>
<p><em>RetrievedColor</em> is a 6-digit RGB value of the current color previously set by this property, or an empty string if the default color is being used.</p>
<p><em>NewColor</em> is one of the 16 primary <a href="../misc/Colors.htm">HTML color names</a>, a hexadecimal RGB color value (the 0x prefix is optional), a pure numeric RGB color value, or the word Default (or an empty string) for its default color. Example values: <code>"Silver"</code>, <code>"FFFFAA"</code>, <code>0xFFFFAA</code>, <code>"Default"</code>, <code>""</code>.</p>
<p>By default, the window's background color is the system's color for the face of buttons.</p>
<p>The color of the <a href="#MenuBar">menu bar</a> and its submenus can be changed as in this example: <code>MyMenuBar.<a href="Menu.htm#SetColor">SetColor</a> "White"</code>.</p>
<p>To make the background transparent, use <a href="../commands/WinSetTransColor.htm">WinSetTransColor</a>. However, if you do this without first having assigned a custom window via <a href="#BackColor">Gui.BackColor</a>, buttons will also become transparent. To prevent this, first assign a custom color and then make that color transparent. 例如:</p>
<pre>MyGui.BackColor := "EEAA99"
WinSetTransColor("EEAA99", MyGui)</pre>
<p>To additionally remove the border and title bar from a window with a transparent background, use the following: <code>MyGui.Opt("-Caption")</code></p>
<p>To illustrate the above, there is an example of an on-screen display (OSD) near the bottom of this page.</p>
</div>

<div class="methodShort" id="MarginX">
<h2>MarginX</h2>
<p>Retrieves or sets the size of horizontal margins between sides and subsequently created controls.</p>
<pre class="Syntax">RetrievedValue := MyGui.MarginX</pre>
<pre class="Syntax">MyGui.MarginX := NewValue</pre>
<p><em>RetrievedValue</em> is the number of pixels of the current horizontal margin.</p>
<p><em>NewValue</em> is the number of pixels of space to leave at the left and right side of the window when auto-positioning any control that lacks an explicit <a href="#XY">X coordinate</a>. Also, the margin is used to determine the horizontal distance that separates auto-positioned controls from each other. Finally, the margin is taken into account by the first use of <a href="#Show">MyGui.Show</a> to calculate the window's size (when no explicit size is specified).</p>
<p>By default, this margin is proportional to the size of the currently selected <a href="#SetFont">font</a> (1.25 times font-height for left &amp; right).</p>
</div>

<div class="methodShort" id="MarginY">
<h2>MarginY</h2>
<p>Retrieves or sets the size of vertical margins between sides and subsequently created controls.</p>
<pre class="Syntax">RetrievedValue := MyGui.MarginY</pre>
<pre class="Syntax">MyGui.MarginY := NewValue</pre>
<p><em>RetrievedValue</em> is the number of pixels of the current vertical margin.</p>
<p><em>NewValue</em> is the number of pixels of space to leave at the top and bottom side of the window when auto-positioning any control that lacks an explicit <a href="#XY">Y coordinate</a>. Also, the margin is used to determine the vertical distance that separates auto-positioned controls from each other. Finally, the margin is taken into account by the first use of <a href="#Show">MyGui.Show</a> to calculate the window's size (when no explicit size is specified).</p>
<p>By default, this margin is proportional to the size of the currently selected <a href="#SetFont">font</a> (0.75 times font-height for top &amp; bottom).</p>
</div>

<div class="methodShort" id="Opt">
<h2>Opt</h2>
<p>Sets one or more options for the GUI window.</p>
<pre class="Syntax">MyGui.<span class="func">Opt</span>(Options)</pre>
<dl>
  <dt>Options</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
  <p>For performance reasons, it is better to set all options in a single line, and to do so before creating the window (that is, before any use of other methods such as <a href="#Add">MyGui.Add</a>).</p>
  <p>The effect of this parameter is cumulative; that is, it alters only those settings that are explicitly specified, leaving all the others unchanged.</p>
  <p>Specify a plus sign to add the option and a minus sign to remove it. 例如: <code>MyGui.Opt("+Resize -MaximizeBox")</code>.</p>
  <p><strong>AlwaysOnTop</strong>: Makes the window stay on top of all other windows, which is the same effect as <a href="../commands/WinSetAlwaysOnTop.htm">WinSetAlwaysOnTop</a>.</p>
  <p><strong>Border</strong>: Provides a thin-line border around the window. This is not common.</p>
  <p><strong>Caption</strong> (present by default): Provides a title bar and a thick window border/edge. When removing the caption from a window that will use <a href="../commands/WinSetTransColor.htm">WinSetTransColor</a>, remove it only after setting the TransColor.</p>
  <p><strong>Disabled</strong>: Disables the window, which prevents the user from interacting with its controls. This is often used on a window that owns other windows (see <a href="#Owner">Owner</a>).</p>
  <p id="DPIScale"><strong>DPIScale</strong>: Use <code>MyGui.Opt("-DPIScale")</code> to disable DPI scaling, which is enabled by default. If DPI scaling is enabled on a system with a non-standard DPI setting, the Gui object and <a href="GuiControl.htm">GuiControl object</a> automatically scale coordinates and sizes to give controls roughly the same apparent size (but higher resolution). For example, with a DPI of 144 (150%), <code>E := MyGui.Add("Edit", "w100")</code> would make the GUI control 150 pixels wide, but <code>E.GetPos(,,W)</code> would still set <em>W</em> to 100. <a href="../Variables.htm#ScreenDPI">A_ScreenDPI</a> contains the system's current DPI.</p>
  <p>DPI scaling only applies to the Gui object and <a href="GuiControl.htm">GuiControl object</a>, so coordinates coming directly from other sources such as ControlGetPos or WinGetPos will not work. There are a number of ways to deal with this:</p>
  <ul>
    <li>Avoid using hard-coded coordinates wherever possible.  例如, use the <a href="#xp">xp</a>, <a href="#xs">xs</a>, <a href="#xm">xm</a> and <a href="#PosPlusMargin">x+m</a> options for positioning controls and specify height in <a href="#R">rows of text</a> instead of pixels.</li>
    <li>Enable (<code>Gui.Opt("+DPIScale")</code>) and disable (<code>Gui.Opt("-DPIScale")</code>) scaling on the fly, as needed. Changing the setting does not affect positions or sizes which have already been set.</li>
    <li>Manually scale the coordinates. 例如, <code>x*(A_ScreenDPI/96)</code> converts x from logical/GUI coordinates to physical/non-GUI coordinates.</li>
  </ul>
  <p id="LastFound"><strong>LastFound</strong>: Sets the window to be the <a href="../misc/WinTitle.htm#LastFoundWindow">last found window</a> (though this is unnecessary in a <a href="GuiOnEvent.htm#Threads">GUI thread</a> because it is done automatically), which allows functions such as <a href="../commands/WinGetStyle.htm">WinGetStyle</a> and <a href="../commands/WinSetTransparent.htm">WinSetTransparent</a> to operate on it even if it is hidden (that is, <a href="../commands/DetectHiddenWindows.htm">DetectHiddenWindows</a> is not necessary). This is especially useful for changing the properties of the window before showing it. 例如:</p>
  <pre>MyGui.Opt(&quot;+LastFound&quot;)
WinSetTransColor(CustomColor " 150", MyGui)
MyGui.Show()</pre>
  <p id="MaximizeBox"><strong>MaximizeBox</strong>: Enables the maximize button in the title bar. This is also included as part of <em>Resize</em> below.</p>
  <p id="MinimizeBox"><strong>MinimizeBox</strong> (present by default): Enables the minimize button in the title bar.</p>
  <p id="MinSize"><strong>MinSize</strong> and <strong>MaxSize</strong>: Determines the minimum and/or maximum size of the window, such as when the user drags its edges to resize it. Specify the word <em>MinSize</em> and/or <em>MaxSize</em> with no suffix to use the window's current size as the limit (if the window has no current size, it will use the size from the first use of <a href="#Show">MyGui.Show</a>). Alternatively, append the width, followed by an X, followed by the height; 例如: <code>Gui.Opt("+Resize +MinSize640x480")</code>. The dimensions are in pixels, and they specify the size of the window's client area (which excludes borders, title bar, and <a href="#MenuBar">menu bar</a>). Specify each number as decimal, not hexadecimal.</p>
  <p>Either the width or the height may be omitted to leave it unchanged (e.g. <code>+MinSize640x</code> or <code>+MinSizex480</code>). Furthermore, Min/MaxSize can be specified more than once to use the window's current size for one dimension and an explicit size for the other. 例如, <code>+MinSize +MinSize640x</code> would use the window's current size for the height and 640 for the width.</p>
  <p>If <em>MinSize</em> and <em>MaxSize</em> are never used, the operating system's defaults are used (similarly, <code>MyGui.Opt("-MinSize -MaxSize")</code> can be used to return to the defaults). Note: the window must have <a href="#Resize">+Resize</a> to allow resizing by the user.</p>
  <p id="OwnDialogs"><strong>OwnDialogs</strong>: <code>MyGui.Opt("+OwnDialogs")</code> should be specified in each <a href="../misc/Threads.htm">thread</a> (such as a event handling function of a Button control) for which subsequently displayed <a href="../commands/MsgBox.htm">MsgBox</a>, <a href="../commands/InputBox.htm">InputBox</a>, <a href="../commands/FileSelect.htm">FileSelect</a>, and <a href="../commands/DirSelect.htm">DirSelect</a> dialogs should be owned by the window. Such dialogs are modal, meaning that the user cannot interact with the GUI window until dismissing the dialog. By contrast, <a href="../commands/ToolTip.htm">ToolTip</a> do not become modal even though they become owned; they will merely stay always on top of their owner. In either case, any owned dialog or window is automatically destroyed when its GUI window is <a href="#Destroy">destroyed</a>.</p>
  <p>There is typically no need to turn this setting back off because it does not affect other <a href="../misc/Threads.htm">threads</a>. However, if a thread needs to display both owned and unowned dialogs, it may turn off this setting via <code>MyGui.Opt("-OwnDialogs")</code>.</p>
  <p id="Owner"><strong>Owner</strong>: Use <em>+Owner</em> to make the window owned by another. An owned window has no taskbar button by default, and when visible it is always on top of its owner. It is also automatically destroyed when its owner is destroyed. <em>+Owner</em> can be used before or after the owned window is created. There are two ways to use <em>+Owner</em>, as shown below:</p>
  <pre>MyGui.Opt("+Owner" OtherGui.Hwnd)  <em>; Make the GUI owned by <i>OtherGui</i>.</em>
MyGui.Opt("+Owner")  <em>; Make the GUI owned by <a href="../Variables.htm#ScriptHwnd">the script's main window</a> to prevent display of a taskbar button.</em></pre>
  <p><code>+Owner</code> can be immediately followed by the <a href="#Hwnd">HWND</a> of any top-level window.</p>
  <p>To prevent the user from interacting with the owner while one of its owned window is visible, disable the owner via <code>MyGui.Opt("+Disabled")</code>. Later (when the time comes to cancel or destroy the owned window), re-enable the owner via <code>MyGui.Opt("-Disabled")</code>. Do this prior to cancel/destroy so that the owner will be reactivated automatically.</p>
  <p id="Parent"><strong>Parent</strong>: Use <code>+Parent</code> immediately followed by the <a href="#Hwnd">HWND</a> of any window or control to use it as the parent of this window. To convert the GUI back into a top-level window, use <code>-Parent</code>. This option works even after the window is created.</p>
  <p id="Resize"><strong>Resize</strong>: Makes the window resizable and enables its maximize button in the title bar. To avoid enabling the maximize button, specify <code>+Resize -MaximizeBox</code>.</p>
  <p><strong>SysMenu</strong> (present by default): Specify <code>-SysMenu</code> (minus SysMenu) to omit the system menu and icon in the window's upper left corner. This will also omit the minimize, maximize, and close buttons in the title bar.</p>
  <p id="Theme"><strong>Theme</strong>: By specifying <code>-Theme</code>, all subsequently created controls in the window will have the Classic Theme appearance. To later create additional controls that obey the current theme, turn it back on via <code>+Theme</code>. Note: This option has no effect if the Classic Theme is in effect. Finally, this setting may be changed for an individual control by specifying <code>+Theme</code> or <code>-Theme</code> in its options when it is created.</p>
  <p><strong>ToolWindow</strong>: Provides a narrower title bar but the window will have no taskbar button. This always hides the maximize and minimize buttons, regardless of whether the <a href="../misc/Styles.htm#WS_MAXIMIZEBOX">WS_MAXIMIZEBOX</a> and <a href="../misc/Styles.htm#WS_MINIMIZEBOX">WS_MINIMIZEBOX</a> styles are present.</p>
  <p><strong>(Unnamed Style)</strong>: Specify a plus or minus sign followed immediately by a decimal or hexadecimal <a href="../misc/Styles.htm">style number</a>.</p>
  <p><strong>(Unnamed ExStyle)</strong>: Specify a plus or minus sign followed immediately by the letter E and a decimal or hexadecimal extended style number. 例如, <code>+E0x40000</code> would add the WS_EX_APPWINDOW style, which provides a taskbar button for a window that would otherwise lack one. Although the other extended styles are not documented here (since they are rarely used), they can be discovered by searching for WS_EX_APPWINDOW at <a href="http://www.microsoft.com">www.microsoft.com</a>.</p></dd>
</dl>
</div>

<div class="methodShort" id="FocusedCtrl">
<h2>FocusedCtrl</h2>
<p>Retrieves the <a href="GuiControl.htm">GuiControl object</a> of the GUI's focused control.</p>
<pre class="Syntax">GuiCtrlObj := MyGui.FocusedCtrl</pre>
<p>Note: To be effective, the window generally must not be minimized or hidden.</p>
</div>

<div class="methodShort" id="MenuBar">
<h2>MenuBar</h2>
<p>Retrieves or sets the window's menu bar.</p>
<pre class="Syntax">MyGui.MenuBar := Bar</pre>
<pre class="Syntax">Bar := MyGui.MenuBar</pre>
<p><em>Bar</em> is a <a href="Menu.htm">MenuBar object</a> created by <a href="Menu.htm#New">MenuBar.New()</a>. For example:</p>
<pre>FileMenu := Menu.New()
FileMenu.Add "&amp;Open`tCtrl+O", (*) =&gt; FileSelect()  <em>; See remarks below about Ctrl+O.</em>
FileMenu.Add "E&amp;xit", (*) =&gt; ExitApp()
HelpMenu := Menu.New()
HelpMenu.Add "&amp;About", (*) =&gt; MsgBox("Not implemented")
Menus := Menu.New()
Menus.Add "&amp;File", FileMenu  <em>; Attach the two submenus that were created above.</em>
Menus.Add "&amp;Help", HelpMenu
MyGui := Gui.New()
MyGui.MenuBar := Menus
MyGui.Show "w300 h200"</pre>
<p>In the first line above, notice that <code>&amp;Open</code> is followed by <code>Ctrl+O</code> (with a tab character in between). This indicates a keyboard shortcut that the user may press instead of selecting the menu item. If the shortcut uses only the standard modifier key names Ctrl, Alt and Shift, it is automatically registered as a <em>keyboard accelerator</em> for the GUI. Single-character accelerators with no modifiers are case-sensitive and can be triggered by unusual means such as IME or <kbd>Alt</kbd>+NNNN.</p>
<p>If a particular key combination does not work automatically, the use of a <a href="../commands/_HotIf.htm">context-sensitive hotkey</a> may be required. However, such hotkeys typically cannot be triggered by <a href="../commands/Send.htm">Send</a> and are more likely to interfere with other scripts than a standard keyboard accelerator.</p>
<p>To remove a window's current menu bar, use <code>MyGui.MenuBar := ""</code> (that is, assign an empty string).</p>
</div>

<div class="methodShort" id="Minimize">
<h2>Minimize</h2>
<p>Unhides the window (if necessary) and minimizes it.</p>
<pre class="Syntax">MyGui.<span class="func">Minimize</span>()</pre>
</div>

<div class="methodShort" id="Maximize">
<h2>Maximize</h2>
<p>Unhides the window (if necessary) and maximizes it.</p>
<pre class="Syntax">MyGui.<span class="func">Maximize</span>()</pre>
</div>

<div class="methodShort" id="Restore">
<h2>Restore</h2>
<p>Unhides the window (if necessary) and restores it, if it was minimized or maximized beforehand.</p>
<pre class="Syntax">MyGui.<span class="func">Restore</span>()</pre>
</div>

<div class="methodShort" id="Move">
<h2>Move</h2>
<p>Moves and/or resizes the GUI window.</p>
<pre class="Syntax">MyGui.<span class="func">Move</span>(<span class="optional">X, Y, Width, Height</span>)</pre>
<dl>
  <dt>X, Y</dt><dd>
    <p>Type: <a href="../Concepts.htm#numbers">Integer</a></p>
    <p>The new position, in screen coordinates.</p></dd>
  <dt>Width, Height</dt><dd>
    <p>Type: <a href="../Concepts.htm#numbers">Integer</a></p>
    <p>The new size.</p></dd>
</dl>
<p>Unlike <a href="../commands/WinMove.htm">WinMove</a>, this method applies <a href="Gui.htm#DPIScale">DPI scaling</a> to <em>Width</em> and <em>Height</em> (unless the <code>-DPIScale</code> option was used).</p>
<p>Examples:</p>
<pre>MyGui.Move(10, 20, 200, 100)
MyGui.Move(VarX+10, VarY+5, VarW*2, VarH*1.5)

<em>; Expand the left and right side by 10 pixels.</em>
MyGui.GetPos(x,, w)
MyGui.Move(x-10,, w+20)</pre>
</div>

<div class="methodShort" id="GetPos">
<h2>GetPos</h2>
<p>Retrieves the position and size of the window.</p>
<pre class="Syntax">MyGui.<span class="func">GetPos</span>(<span class="optional">X, Y, Width, Height</span>)</pre>
<p>Each parameter should be a variable in which to store the respective coordinate. The coordinates are the upper left corner of the window. X and Y are in screen coordinates. Width is the horizontal distance between the left and right side of the window, and height the vertical distance between the top and bottom side (in pixels).</p>
<p>Unlike <a href="../commands/WinGetPos.htm">WinGetPos</a>, this method applies <a href="#DPIScale">DPI scaling</a> to <em>Width</em> and <em>Height</em> (unless the <code>-DPIScale</code> option was used).</p>
</div>

<div class="methodShort" id="GetClientPos">
<h2>GetClientPos</h2>
<p>Retrieves the position and size of the window's client area.</p>
<pre class="Syntax">MyGui.<span class="func">GetClientPos</span>(<span class="optional">X, Y, Width, Height</span>)</pre>
<p>Each parameter should be a variable in which to store the respective coordinate. The coordinates are the upper left corner of the window's client area, which is the area not including title bar, menu bar, and borders. X and Y are in screen coordinates. Width is the horizontal distance between the left and right side of the client area, and height the vertical distance between the top and bottom side (in pixels).</p>
<p>Unlike <a href="../commands/WinGetClientPos.htm">WinGetClientPos</a>, this method applies <a href="#DPIScale">DPI scaling</a> to <em>Width</em> and <em>Height</em> (unless the <code>-DPIScale</code> option was used).</p>
</div>

<div class="methodShort" id="Flash">
<h2>Flash</h2>
<p>Blinks the window's button in the taskbar.</p>
<pre class="Syntax">MyGui.<span class="func">Flash</span>(<span class="optional">Blink := true</span>)</pre>
<dl>
  <dt>Blink</dt><dd>
    <p>类型: <a href="../Concepts.htm#boolean">整数(布尔值)</a></p>
    <p>If this parameter is omitted or 1 (true), the window's button in the taskbar will blink. This is done by inverting the color of the window's title bar and/or taskbar button (if it has one). Specify 0 (false) to restore the original colors of the title bar and taskbar button (but the actual behavior might vary depending on OS version).</p>
  </dd>
</dl>
<p>In the below example, the window will blink three times because each pair of flashes inverts then restores its appearance:</p>
<pre>Loop 6
{
    MyGui.Flash
    Sleep 500  <em>; It's quite sensitive to this value; altering it may change the behavior in unexpected ways.</em>
}</pre>
</div>

<div class="methodShort" id="Hwnd">
<h2>Hwnd</h2>
<p>Retrieves the window handle (HWND) of the GUI window.</p>
<pre class="Syntax">CurrentHwnd := MyGui.Hwnd</pre>
<p>A GUI's HWND is often used with <a href="../commands/PostMessage.htm">PostMessage</a>, <a href="../commands/SendMessage.htm">SendMessage</a>, and <a href="../commands/DllCall.htm">DllCall</a>. It can also be used directly as an <a href="../misc/WinTitle.htm#ahk_id">ahk_id WinTitle</a>.</p></div>

<div class="methodShort" id="Title">
<h2>Title</h2>
<p>Retrieves or sets the GUI's title.</p>
<pre class="Syntax">RetrievedTitle := MyGui.Title</pre>
<pre class="Syntax">MyGui.Title := NewTitle</pre>
</div>

<div class="methodShort" id="Name">
<h2>Name</h2>
<p>Retrieves or sets a custom name for the GUI window.</p>
<pre class="Syntax">RetrievedName := MyGui.Name</pre>
<pre class="Syntax">MyGui.Name := NewName</pre>
</div>

<div class="methodShort" id="__Item">
<h2>__Item</h2>
<p>Retrieves the <a href="GuiControl.htm">GuiControl object</a> associated with the specified name, text, ClassNN or HWND.</p>
<pre class="Syntax">GuiCtrlObj := Gui[Name]</pre>
</div>

<div class="methodShort" id="__Enum">
<h2>__Enum</h2>
<p>Enumerates the GUI's controls.</p>
<pre class="Syntax">For Hwnd <span class="optional">, Ctrl</span> in Gui</pre>
<p>Returns a new <a href="Enumerator.htm">enumerator</a>. This method is typically not called directly. Instead, the Gui object is passed directly to a <a href="../commands/For.htm">for-loop</a>, which calls __Enum once and then calls the enumerator once for each iteration of the loop. Each call to the enumerator returns the next control. The for-loop's variables correspond to the enumerator's parameters, which are:</p>
<dl>
  <dt>Hwnd</dt>
  <dd>
    <p>Type: <a href="../Concepts.htm#numbers">Integer</a></p>
    <p>The control's HWND.</p>
  </dd>
  <dt>Ctrl</dt>
  <dd>
    <p>The <a href="GuiControl.htm">GuiControl</a> object.</p>
  </dd>
</dl>
<p>For example:</p>
<pre>For Hwnd, GuiCtrlObj in MyGui
    MsgBox "Control #" A_Index " is " GuiCtrlObj.ClassNN</pre>
</div>

<h2 id="Navigate">Keyboard Navigation</h2>
<p>A GUI window may be navigated via the <kbd>Tab</kbd> key, which moves keyboard focus to the next input-capable control (controls from which the <a href="#Tabstop">Tabstop</a> style has been removed are skipped). The order of navigation is determined by the order in which the controls were originally added. When the window is shown for the first time, the first input-capable control that has the Tabstop style (which most control types have by default) will have keyboard focus.</p>
<p id="ShortcutKey">Certain controls may contain an ampersand (&amp;) to create a keyboard shortcut, which might be displayed in the control's text as an underlined character (depending on system settings). A user activates the shortcut by holding down the <kbd>Alt</kbd> key then typing the corresponding character. For buttons, checkboxes,  and radio buttons, pressing the shortcut is the same as clicking the control. For GroupBoxes and Text controls, pressing the shortcut causes keyboard focus to jump to the first input-capable <a href="#Tabstop">tabstop</a> control that was created after it. However, if more than one control has the same shortcut key, pressing the shortcut will alternate keyboard focus among all controls with the same shortcut.</p>
<p>To display a literal ampersand inside the control types mentioned above, specify two consecutive ampersands as in this example: <code>MyGui.Add("Button",, "Save &amp;&amp; Exit")</code>.</p>

<h2 id="Appear">Window Appearance</h2>
<p>For its icon, a GUI window uses the tray icon that was in effect at the time the window was created. Thus, to have a different icon, change the tray icon before creating the window. For example: <code><a href="../commands/TraySetIcon.htm">TraySetIcon</a>("MyIcon.ico")</code>. It is also possible to have a different large icon for a window than its small icon (the large icon is displayed in the alt-tab task switcher). This can be done via <a href="../commands/LoadPicture.htm">LoadPicture</a> and <a href="../commands/SendMessage.htm">SendMessage</a>; for example:</p>
<pre>iconsize := 32  <em>; Ideal size for alt-tab varies between systems and OS versions.</em>
hIcon := LoadPicture("My Icon.ico", "Icon1 w" iconsize " h" iconsize, imgtype)
MyGui := Gui.New()
SendMessage(0x80, 1, hIcon, MyGui)  <em>; 0x80 is WM_SETICON; and 1 means ICON_BIG (vs. 0 for ICON_SMALL).</em>
MyGui.Show()</pre>
<p>Due to OS limitations, Checkboxes, Radio buttons, and GroupBoxes for which a non-default text color was specified will take on the Classic Theme appearance.</p>
<p>Related topic: <a href="#MarginX">window's margin</a>.</p>

<h2 id="GenRemarks">General Remarks</h2>
<p>Use the <a href="GuiControl.htm">GuiControl object</a> to operate upon individual controls in a GUI window.</p>
<p>Each GUI window may have up to 11,000 controls. However, use caution when creating more than 5000 controls because system instability may occur for certain control types.</p>
<p id="deleted">The GUI window is automatically <a href="#Destroy">destroyed</a> when the Gui object is deleted, which occurs when its <a href="../Objects.htm#Reference_Counting">reference count</a> reaches zero. However, this does not typically occur while the window is visible, as <a href="#Show">Show</a> automatically increments the reference count. While the window is visible, the user can interact with it and raise events which are handled by the script. When the user closes the window or it is hidden by <a href="#Hide">Hide</a>, <a href="#Show">Show</a> or <a href="#Submit">Submit</a>, this extra reference is released.</p>
<p>To keep a GUI window "alive" without calling <a href="#Show">Show</a> or retaining a reference to its Gui object, the script can increment the object's reference count with <a href="../commands/ObjAddRef.htm">ObjAddRef</a> (in which case <a href="../commands/ObjAddRef.htm">ObjRelease</a> must be called when the window is no longer needed). 例如, this might be done when using a hidden GUI window to <a href="../commands/OnMessage.htm">receive messages</a>, or if the window is shown by "external" means such as <a href="../commands/WinShow.htm">WinShow</a> (by this script or any other).</p>
<p>If the script is not <a href="../Scripts.htm#persistent">persistent</a> for any other reason, it will exit after the last visible GUI is closed; either when the last thread completes or immediately if no threads are running.</p>

<h2 id="Related">Related</h2>
<p><a href="GuiControl.htm">GuiControl object</a>, <a href="../commands/GuiFromHwnd.htm">GuiFromHwnd</a>, <a href="../commands/GuiCtrlFromHwnd.htm">GuiCtrlFromHwnd</a>, <a href="../commands/GuiControls.htm">Control Types</a>, <a href="../commands/ListView.htm">ListView</a>, <a href="../commands/TreeView.htm">TreeView</a>, <a href="Menu.htm">Menu object</a>, <a href="../commands/Control.htm">Control functions</a>, <a href="../commands/MsgBox.htm">MsgBox</a>, <a href="../commands/FileSelect.htm">FileSelect</a>, <a href="../commands/DirSelect.htm">DirSelect</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExPopup">
<p><a href="#ExPopup">#1</a>: Display a text pop-up:</p>
<pre>MyGui := Gui.New(, "Title of Window")
MyGui.Opt("+AlwaysOnTop +Disabled -SysMenu +Owner")  <em>; +Owner avoids a taskbar button.</em>
MyGui.Add("Text",, "Some text to display.")
MyGui.Show("NoActivate")  <em>; NoActivate avoids deactivating the currently active window.</em></pre>
</div>

<div class="ex" id="ExInputBox">
<p><a href="#ExInputBox">#2</a>: A simple input-box that asks for the first and last name:</p>
<pre>MyGui := Gui.New(, "Simple Input Example")
MyGui.Add("Text",, "First name:")
MyGui.Add("Text",, "Last name:")
MyGui.Add("Edit", "vFirstName ym")  <em>; The ym option starts a new column of controls.</em>
MyGui.Add("Edit", "vLastName")
MyGui.Add("Button", "default", "OK").OnEvent("Click", (*) =&gt; ProcessUserInput(MyGui))
MyGui.OnEvent("Close", "ProcessUserInput")
MyGui.Show()

ProcessUserInput(this, *)
{
    Saved := this.Submit()  <em>; Save the contents of named controls into an object.</em>
    MsgBox("You entered '" Saved.FirstName " " Saved.LastName "'.")
}</pre>
</div>

<div class="ex" id="ExTab">
<p><a href="#ExTab">#3</a>: Creates a tab control with multiple tabs, each containing different controls to interact with:</p>
<pre>MyGui := Gui.New()
Tab := MyGui.Add("Tab3",, ["First Tab","Second Tab","Third Tab"])
MyGui.Add("Checkbox", "vMyCheckbox", "Sample checkbox") 
Tab.UseTab(2)
MyGui.Add("Radio", "vMyRadio", "Sample radio1")
MyGui.Add("Radio",, "Sample radio2")
Tab.UseTab(3)
MyGui.Add("Edit", "vMyEdit r5")  <em>; r5 means 5 rows tall.</em>
Tab.UseTab()  <em>; i.e. subsequently-added controls will not belong to the tab control.</em>
Btn := MyGui.Add("Button", "default xm", "OK")  <em>; xm puts it at the bottom left corner.</em>
Btn.OnEvent("Click", (*) =&gt; ProcessUserInput(MyGui))
MyGui.OnEvent("Close", "ProcessUserInput")
MyGui.OnEvent("Escape", "ProcessUserInput")
MyGui.Show()

ProcessUserInput(this, *)
{
    Saved := this.Submit()  <em>; Save the contents of named controls into an object.</em>
    MsgBox("You entered:`n" Saved.MyCheckbox "`n" Saved.MyRadio "`n" Saved.MyEdit)
}</pre>
</div>

<div class="ex" id="ExListBox">
<p><a href="#ExListBox">#4</a>: Creates a ListBox control containing files in a directory:</p>
<pre>MyGui := Gui.New()
MyGui.Add("Text",, "Pick a file to launch from the list below.")
LB := MyGui.Add("ListBox", "w640 r10 vFile")
LB.OnEvent("DoubleClick", "LaunchFile")
Loop Files, "C:\*.*"  <em>; Change this folder and wildcard pattern to suit your preferences.</em>
    LB.Add(A_LoopFilePath)
MyGui.Add("Button", "Default", "OK").OnEvent("Click", "LaunchFile")
MyGui.Show()

LaunchFile(this, *)
{
    Saved := this.Gui.Submit()
    if MsgBox("Would you like to launch the file or document below?`n`n" Saved.File,, 4) = "No"
        return
    <em>; Otherwise, try to launch it:</em>
    try Run(Saved.File)
    if A_LastError
        MsgBox("Could not launch the specified file. Perhaps it is not associated with anything.")
}</pre>
</div>

<div class="ex" id="ExToolTip">
<p><a href="#ExToolTip">#5</a>: Displays a context-sensitive help (via ToolTip) whenever the user moves the mouse over a particular control:</p>
<pre>
MyGui := Gui.New()
MyEdit := MyGui.Add("Edit")
<em>; Store the tooltip text in a custom property:</em>
MyEdit.ToolTip := "This is a tooltip for the control whose name is MyEdit."
MyDDL := MyGui.Add("DropDownList", "", ["Red","Green","Blue"])
MyDDL.ToolTip := "Choose a color from the drop-down list."
MyGui.Add("Checkbox", "vMyCheckBox", "This control has no tooltip.")
MyGui.OnEvent("Close", (*) => ExitApp())
MyGui.Show()
OnMessage(0x200, "WM_MOUSEMOVE")

WM_MOUSEMOVE(wParam, lParam, msg, Hwnd)
{
    static PrevHwnd := 0
    if (Hwnd != PrevHwnd)
    {
        Text := "", ToolTip() <em>; Turn off any previous tooltip.</em>
        CurrControl := GuiCtrlFromHwnd(Hwnd)
        if CurrControl
        {
            if !CurrControl.HasProp("ToolTip")
                return <em>; No tooltip for this control.</em>
            Text := CurrControl.ToolTip
            SetTimer () => ToolTip(Text), -1000
            SetTimer () => ToolTip(), -4000 <em>; Remove the tooltip.</em>
        }
        PrevHwnd := Hwnd
    }
}</pre>
</div>

<div class="ex" id="ExOSD">
<p><a href="#ExOSD">#6</a>: Creates an On-screen display (OSD) via transparent window:</p>
<pre>MyGui := Gui.New()
MyGui.Opt("+AlwaysOnTop -Caption +ToolWindow")  <em>; +ToolWindow avoids a taskbar button and an alt-tab menu item.</em>
MyGui.BackColor := "EEAA99"  <em>; Can be any RGB color (it will be made transparent below).</em>
MyGui.SetFont("s32")  <em>; Set a large font size (32-point).</em>
CoordText := MyGui.Add("Text", "cLime", "XXXXX YYYYY")  <em>; XX &amp; YY serve to auto-size the window.
; Make all pixels of this color transparent and make the text itself translucent (150):</em>
WinSetTransColor(MyGui.BackColor " 150", MyGui)
SetTimer(() =&gt; UpdateOSD(CoordText), 200)
UpdateOSD(CoordText)  <em>; Make the first update immediate rather than waiting for the timer.</em>
MyGui.Show("x0 y400 NoActivate")  <em>; NoActivate avoids deactivating the currently active window.</em>

UpdateOSD(GuiCtrl)
{
    MouseGetPos(MouseX, MouseY)
    GuiCtrl.Value := "X" MouseX ", Y" MouseY
}</pre>
</div>

<div class="ex" id="ExProgressBar">
<p><a href="#ExProgressBar">#7</a>: A moving progress bar overlayed on a background image:</p>
<pre>MyGui := Gui.New()
MyGui.BackColor := "White"
MyGui.Add("Picture", "x0 y0 h350 w450", A_WinDir "\Web\Wallpaper\Windows\img0.jpg")
MyBtn := MyGui.Add("Button", "Default xp+20 yp+250", "Start the Bar Moving")
MyBtn.OnEvent("Click", "MoveBar")
MyGui.Add("Progress", "w416 vMyProgress")
MyGui.Add("Text", "wp vMyText")  <em>; wp means "use width of previous".</em>
MyGui.Show()

MoveBar(this, *)
{
    ProgressCtrl := this.Gui["MyProgress"]
    TextCtrl := this.Gui["MyText"]
    Loop Files, A_WinDir "\*.*"
    {
        if (A_Index &gt; 100)
            break
        ProgressCtrl.Value := A_Index
        TextCtrl.Value := A_LoopFileName
        Sleep 50
    }
    TextCtrl.Value := "Bar finished."
}</pre>
</div>

<div class="ex" id="ExImageViewer">
<p><a href="#ExImageViewer">#8</a>: A simple image viewer:</p>
<pre>MyGui := Gui.New("+Resize")
MyBtn := MyGui.Add("Button", "default", "&amp;Load New Image")
MyBtn.OnEvent("Click", "LoadNewImage")
MyGui.Add("Radio", "ym+5 x+10 checked vMyRadio", "Load &amp;actual size")
MyGui.Add("Radio", "ym+5 x+10", "Load to &amp;fit screen")
MyGui.Add("Pic", "xm vMyPic")
MyGui.Show()

LoadNewImage(this, *)
{
    RadioCtrl := this.Gui["MyRadio"]
    PicCtrl := this.Gui["MyPic"]
    File := FileSelect(,, "Select an image:", "Images (*.gif; *.jpg; *.bmp; *.png; *.tif; *.ico; *.cur; *.ani; *.exe; *.dll)")
    if File = ""
        return
    if RadioCtrl.Value = 1  <em>; Display image at its actual size.</em>
    {
        Width := 0
        Height := 0
    }
    else <em>; Second radio is selected: Resize the image to fit the screen.</em>
    {
        Width := A_ScreenWidth - 28  <em>; Minus 28 to allow room for borders and margins inside.</em>
        Height := -1  <em>; "Keep aspect ratio" seems best.</em>
    }
    PicCtrl.Value := Format("*w{1} *h{2} {3}", Width, Height, File)  <em>; Load the image.</em>
    this.Gui.Title := File
    this.Gui.Show("xCenter y0 AutoSize")  <em>; Resize the window to match the picture size.</em>
}</pre>
</div>

<div class="ex" id="ExEditor">
<p><a href="#ExEditor">#9</a>: A simple text editor with menu bar:</p>
<pre><em>; Make these variables accessible for all the functions below:</em>
global MyGui, FileMenu, MainEdit, CurrentFileName, About

<em>; Create the MyGui window:</em>
MyGui := Gui.New("+Resize", "Untitled")  <em>; Make the window resizable.</em>

<em>; Create the submenus for the menu bar:</em>
FileMenu := Menu.New()
FileMenu.Add("&amp;New", "MenuFileNew")
FileMenu.Add("&amp;Open", "MenuFileOpen")
FileMenu.Add("&amp;Save", "MenuFileSave")
FileMenu.Add("Save &amp;As", "MenuFileSaveAs")
FileMenu.Add() <em>; Separator line.</em>
FileMenu.Add("E&amp;xit", "MenuFileExit")
HelpMenu := Menu.New()
HelpMenu.Add("&amp;About", "MenuHelpAbout")

<em>; Create the menu bar by attaching the submenus to it:</em>
MyMenuBar := MenuBar.New()
MyMenuBar.Add("&amp;File", FileMenu)
MyMenuBar.Add("&amp;Help", HelpMenu)

<em>; Attach the menu bar to the window:</em>
MyGui.MenuBar := MyMenuBar

<em>; Create the main Edit control:</em>
MainEdit := MyGui.Add("Edit", "WantTab W600 R20")

<em>; Apply events:</em>
MyGui.OnEvent("DropFiles", "Gui_DropFiles")
MyGui.OnEvent("Size", "Gui_Size")

MenuFileNew()  <em>; Apply default settings.</em>
MyGui.Show()  <em>; Display the window.</em>

MenuFileNew(*)
{
    MainEdit.Value := ""  <em>; Clear the Edit control.</em>
    FileMenu.Disable("3&amp;")  <em>; Gray out &amp;Save.</em>
    MyGui.Title := "Untitled"
}

MenuFileOpen(*)
{
    MyGui.Opt("+OwnDialogs")  <em>; Force the user to dismiss the FileSelect dialog before returning to the main window.</em>
    SelectedFileName := FileSelect(3,, "Open File", "Text Documents (*.txt)")
    if SelectedFileName = "" <em>; No file selected.</em>
        return
    CurrentFileName := readContent(SelectedFileName)
}

MenuFileSave(*)
{
    saveContent(CurrentFileName)
}

MenuFileSaveAs(*)
{
    MyGui.Opt("+OwnDialogs")  <em>; Force the user to dismiss the FileSelect dialog before returning to the main window.</em>
    SelectedFileName := FileSelect("S16",, "Save File", "Text Documents (*.txt)")
    if SelectedFileName = "" <em>; No file selected.</em>
        return
    CurrentFileName := saveContent(SelectedFileName)
}

MenuFileExit(*)  <em>; User chose "Exit" from the File menu.</em>
{
    WinClose()
}

MenuHelpAbout(*)
{
    About := Gui.New("+owner" MyGui.Hwnd)  <em>; Make the main window the owner of the "about box".</em>
    MyGui.Opt("+Disabled")  <em>; Disable main window.</em>
    About.Add("Text",, "Text for about box.")
    About.Add("Button", "Default", "OK").OnEvent("Click", "About_Close")
    About.OnEvent("Close", "About_Close")
    About.OnEvent("Escape", "About_Close")
    About.Show()
}

readContent(FileName)
{
    try
        FileContent := FileRead(FileName)  <em>; Read the file's contents into the variable.</em>
    catch
    {
        MsgBox("Could not open '" FileName "'.")
        return
    }
    MainEdit.Value := FileContent  <em>; Put the text into the control.</em>
    FileMenu.Enable("3&amp;")  <em>; Re-enable &amp;Save.</em>
    MyGui.Title := FileName  <em>; Show file name in title bar.</em>
    return FileName
}

saveContent(FileName)
{
    try
    {
        if FileExist(FileName)
            FileDelete(FileName)
        FileAppend(MainEdit.Value, FileName)  <em>; Save the contents to the file.</em>
    }
    catch
    {
        MsgBox("The attempt to overwrite '" FileName "' failed.")
        return
    }
    <em>; Upon success, Show file name in title bar (in case we were called by MenuFileSaveAs):</em>
    MyGui.Title := FileName
    return FileName
}

About_Close(*)
{
    MyGui.Opt("-Disabled")  <em>; Re-enable the main window (must be done prior to the next step).</em>
    About.Destroy()  <em>; Destroy the about box.</em>
}

Gui_DropFiles(ThisGui, Ctrl, FileArray, *)  <em>; Support drag &amp; drop.</em>
{
    CurrentFileName := readContent(FileArray[1])  <em>; Read the first file only (in case there's more than one).</em>
}

Gui_Size(ThisGui, MinMax, Width, Height)
{
    if MinMax = -1  <em>; The window has been minimized. No action needed.</em>
        return
    <em>; Otherwise, the window has been resized or maximized. Resize the Edit control to match.</em>
    MainEdit.Move(,, Width-20, Height-20)
}</pre>
</div>

</body>
</html>